﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Search.TxMainSearchControl.TxSearchCommandCollectionModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using UiInternalControls.WPFControls.SearchControl;


namespace DnProcessSimulateCommands.Search.TxMainSearchControl;

internal class TxSearchCommandCollectionModel : TxSearchBaseCollectionModel
{
  private static List<TxSearchItemCommand> _allCommands;
  private static bool _DEBUG_LOG;

  public virtual void Search(string str)
  {
    str = TxSearchModelUtils.CleanStringForSearch(str);
    TxSearchCommandCollectionModel.GenerateCommandsList();
    this.Clear();
    List<TxSearchItemCommand> source1 = new List<TxSearchItemCommand>();
    if (string.IsNullOrEmpty(str))
    {
      source1 = TxSearchCommandCollectionModel._allCommands;
    }
    else
    {
      foreach (TxSearchItemCommand allCommand in TxSearchCommandCollectionModel._allCommands)
      {
        if (allCommand.SearchInAlias(str) || allCommand.Name.ToLower().Contains(str))
          source1.Add(allCommand);
      }
    }
    IEnumerable<TxSearchItemCommand> source2 = (IEnumerable<TxSearchItemCommand>) source1.OrderBy<TxSearchItemCommand, string>((Func<TxSearchItemCommand, string>) (cmd => cmd.Category)).ThenBy<TxSearchItemCommand, string>((Func<TxSearchItemCommand, string>) (cmd => cmd.Name));
    if (source2.Count<TxSearchItemCommand>() <= 0)
      return;
    TxApplicationEx.ApplicationEventsEx.Idle -= new TxApplication_IdleEventHandler(this.appEventsEx_Idle);
    TxApplicationEx.ApplicationEventsEx.Idle += new TxApplication_IdleEventHandler(this.appEventsEx_Idle);
    foreach (TxSearchItemBase txSearchItemBase in source2)
      this.Items.Add(txSearchItemBase);
  }

  private void appEventsEx_Idle(object sender, EventArgs args) => base.Refresh();

  public virtual void Refresh()
  {
    foreach (TxSearchItemCommand searchItemCommand in (Collection<TxSearchItemBase>) this.Items)
      searchItemCommand.UpdateIsCommandEnabled();
  }

  private static void GenerateCommandsList()
  {
    if (TxSearchCommandCollectionModel._allCommands != null)
      return;
    TxCommandManager txCommandManager = new TxCommandManager();
    int index = 0;
    TxSearchCommandCollectionModel._allCommands = new List<TxSearchItemCommand>();
    foreach (string allowedCommand in txCommandManager.GetAllowedCommands())
    {
      ++index;
      TxSearchCommandCollectionModel.LogItCommandId(index, allowedCommand);
      string commandName = txCommandManager.GetCommandName(allowedCommand);
      string commandCategory = txCommandManager.GetCommandCategory(allowedCommand);
      string commandTooltip = txCommandManager.GetCommandTooltip(allowedCommand);
      if (commandName != null)
      {
        TxSearchItemCommand searchItemCommand = new TxSearchItemCommand(commandName, allowedCommand, commandCategory, commandTooltip);
        TxSearchCommandCollectionModel._allCommands.Add(searchItemCommand);
        TxSearchCommandCollectionModel.LogItCommandName(((TxSearchItemBase) searchItemCommand).Caption);
      }
    }
  }

  private static void LogItCommandId(int index, string command)
  {
    if (!TxSearchCommandCollectionModel._DEBUG_LOG)
      return;
    StreamWriter streamWriter = new StreamWriter("c:\\SearchTestFile.txt", true);
    streamWriter.Write($"{index.ToString()}:\t\t{command}\t\t");
    streamWriter.Close();
  }

  private static void LogItCommandName(string commandName)
  {
    if (!TxSearchCommandCollectionModel._DEBUG_LOG)
      return;
    StreamWriter streamWriter = new StreamWriter("c:\\SearchTestFile.txt", true);
    streamWriter.WriteLine(commandName);
    streamWriter.Close();
  }

  protected virtual void Dispose(bool disposing)
  {
    TxApplicationEx.ApplicationEventsEx.Idle -= new TxApplication_IdleEventHandler(this.appEventsEx_Idle);
    base.Dispose(disposing);
  }
}
